using System;
using System.Data;
using System.Data.SqlClient;
using NNS.Framework;
using QLTB.BusinessObjects;

namespace QLTB.DataAccess
{
    /// <summary>
    /// Project: NNS System 
    /// Generated Class for Table : TBL_THIETBI.
    /// Date: 06/08/2010
    /// Author: Trần Hoàng Tâm
    /// </summary>
    public class TBL_THIETBIDA : Object
    {
        public int Count(
            int ID)
        {
            object ob = SqlHelper.Helper.ExecuteScalar(CommandType.Text,
                                                       "Select Count(*) as ID from TBL_THIETBI where  MaTB=@MaTB",
                                                       new[] {new SqlParameter("@MaTB", ID)});
            SqlHelper.Helper.Close();
            return Convert.ToInt32(ob);
        }

        public int CountBy(
            int ID,
            string code)
        {
            object count = SqlHelper.Helper.ExecuteScalar(CommandType.Text,
                                                          "Select Count(*) from TBL_THIETBI where  MaTB=" + code +
                                                          " And ID<>" + ID);
            SqlHelper.Helper.Close();
            return (int) count;
        }

        public TBL_THIETBI Populate(
            IDataReader myReader)
        {
            TBL_THIETBI obj = null;
            if (myReader != null)
            {
                obj = new TBL_THIETBI();
                if (!myReader.IsDBNull(myReader.GetOrdinal("ID")))
                {
                    obj.ID = (int) myReader["ID"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("MaTB")))
                {
                    obj.MaTB = myReader["MaTB"] as string;
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("TenTB")))
                {
                    obj.TenTB = myReader["TenTB"] as string;
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("NSX")))
                {
                    obj.NSX = (int) myReader["NSX"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("MaNCC")))
                {
                    obj.MaNCC = (int) myReader["MaNCC"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("NguyenGia")))
                {
                    obj.NguyenGia = (decimal) myReader["NguyenGia"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("NgaySuDung")))
                {
                    obj.NgaySuDung = (DateTime) myReader["NgaySuDung"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("TinhTrang")))
                {
                    obj.TinhTrang = (int) myReader["TinhTrang"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("MaDonVi")))
                {
                    obj.MaDonVi = (int) myReader["MaDonVi"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("DiaChiIP")))
                {
                    obj.DiaChiIP = myReader["DiaChiIP"] as string;
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("MucDich")))
                {
                    obj.MucDich = (int) myReader["MucDich"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("MaLoaiTB")))
                {
                    obj.MaLoaiTB = (int) myReader["MaLoaiTB"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("TgianBH")))
                {
                    obj.TgianBH = (int) myReader["TgianBH"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("DChiBH")))
                {
                    obj.DChiBH = (int) myReader["DChiBH"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("TgianSD")))
                {
                    obj.TgianSD = (int) myReader["TgianSD"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("ThuocTS")))
                {
                    obj.ThuocTS = (bool) myReader["ThuocTS"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("GhiChu")))
                {
                    obj.GhiChu = myReader["GhiChu"] as string;
                }
            }
            return obj;
        }

        public void Duplicate(int id)
        {
            SqlHelper.Helper.Start();
            SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_THIETBI_Duplicate",
                                             new[] {new SqlParameter("@id", id)});
            if (SqlHelper.Helper.LastError != null)
            {
                MyBox.ShowWarning(SqlHelper.Helper.LastError.Message, "Quản lý thiết bị");
                SqlHelper.Helper.RollBack();
            }
            SqlHelper.Helper.Commit();
        }

        public static string NewID(int MaDonvi)
        {
            string ma = MaDonvi.ToString().Substring(2, 2);
            return SqlHelperSmart.GenCode("TBL_THIETBI", "MaTB", ma, 5);
        }

        #region Exist

        public bool Exist(
            int ID)
        {
            bool Result = false;

            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader(CommandType.Text,
                                                                    "Select * from TBL_THIETBI where  MaTB=@MaTB",
                                                                    new[] {new SqlParameter("@MaTB", ID)});
            if (myReader != null)
            {
                Result = myReader.HasRows;
                myReader.Close();
            }
            SqlHelper.Helper.Close();
            return Result;
        }

        #endregion NewID

        #region Get

        public TBL_THIETBI Get(
            int ID)
        {
            TBL_THIETBI tb = null;
            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader(CommandType.Text,
                                                                    "Select * from TBL_THIETBI where  ID=@ID",
                                                                    new[] {new SqlParameter("@ID", ID)});
            if (myReader != null)
            {
                if (myReader.Read())
                {
                    tb = Populate(myReader);
                }
            }
            SqlHelper.Helper.Close();
            return tb;
        }

        #endregion
    }
}